<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">节点别名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nodeBName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">节点地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nodeHost" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="editPass">修改密码</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: 'node/list',
            request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
             },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                  "code": res.errorCode, //解析接口状态
                  "msg": res.errorMsg, //解析提示文本
                  "count": res.data.total, //解析数据长度
                  "data": res.data.list //解析数据列表
                };
              },
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id',  title: 'ID',hide:true},
                {field: 'nodeBName',  title: '节点别名'},
                {field: 'nodeHost',  title: '节点地址'},
                {field: 'nodePort',  title: '节点端口'},
                {field: 'nodeUser', title: '节点用户'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 20, 30, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/nodeinfo/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '添加',
                    type: 1,
                    shade: 0.2,
                    shadeClose: true,
                    area: ['600px'],
                    content: content,
                    end:function (){
                    	table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                        }, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('page/nodeinfo/edit.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑',
                    type: 1,
                    shade: 0.2,
                    shadeClose: true,
                    area: ['600px'],
                    content: content,
                    success: function (layero, index) {
                       $('#id').val(data.id)
                       $('#nodeBName').val(data.nodeBName)
                       $('#nodeHost').val(data.nodeHost)
                       $('#nodePort').val(data.nodePort)
                       $('#nodeUser').val(data.nodeUser)
                       $('#nodePasw').val("")
                   },
                   end:function (){
                   	table.reload('currentTableId', {
                           page: {
                               curr: 1
                           }
                       }, 'data');
                   }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'editPass') {
                var content = miniPage.getHrefContent('page/nodeinfo/editPass.html');
                var openWH = miniPage.getOpenWidthHeight();
                var index = layer.open({
                    title: '编辑',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['600px'],
                    content: content,
                    success: function (layero, index) {
                       $('#id').val(data.id)
                       $('#nodeBName').val(data.nodeBName)
                       $('#nodeHost').val(data.nodeHost)
                       $('#nodePort').val(data.nodePort)
                       $('#nodeUser').val(data.nodeUser)
                       $('#nodePasw').val("")
                   },
                   end:function (){
                   	table.reload('currentTableId', {
                           page: {
                               curr: 1
                           }
                       }, 'data');
                   }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除?', function (index) {
                	axios({
		        		method:"post",
		        		url:"node/delete",
		        		data:data
		        	}).then(function(response){
		        		var data = response.data
		        		if(data.success){
			        		layer.close(index);
			        		table.reload('currentTableId', {
	                            page: {
	                                curr: 1
	                            }
	                        }, 'data');
		        		}else{
		        			layer.msg(data.errorMsg, {icon: 2});
		        		}
		        	});
                });
            }
        });

    });
</script>